package com.itita.ww2.game.item;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.data.ItemConfig;
import com.itita.ww2.enums.WW2ExceptionCode;
import com.itita.ww2.game.user.UserArmyManager;
import com.itita.ww2.game.user.UserInventoryManager;
import com.itita.ww2.model.army.UserArmy;
import com.itita.ww2.model.item.ds.ItemValue;
import com.itita.ww2.model.item.enums.ItemPropType;
import com.itita.ww2.model.item.prop.ItemProp;
import com.itita.ww2.model.user.UserProfile;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class ItemService {
	private static final Logger logger = LoggerFactory.getLogger(ItemService.class);

	/**
	 * 客户端(玩家)主动使用物品
	 */
	public static final ISFSObject useItemProp(UserProfile userProfile, String uuid, ISFSObject params)
			throws WW2Exception {
		UserInventoryManager manager = userProfile.getInventory();
		ItemValue item = manager.getItemByUUID(uuid);
		if (item == null) {
			return SFSObject.newInstance();
		}
		if (!item.getItem().isUseable() || !(item.getItem() instanceof ItemProp)) {
			logger.error("item: {} isn't an useable item.");
			return SFSObject.newInstance();
		}
		ItemProp useable = (ItemProp) item.getItem();

		ISFSObject ref = useable.use(userProfile, params);
		manager.operItemCount(item, -1);
		return ref;
	}

	/**
	 * 使用道具
	 * @param param1 一级参数,对应propType
	 */
	public static final ISFSObject useItemProp(UserProfile userProfile, ISFSObject params, ItemPropType propType,
			int param1) throws WW2Exception {
		UserInventoryManager manager = userProfile.getInventory();
		ItemValue item = manager.getItemProp(propType, param1);
		if (item == null) {
			return SFSObject.newInstance();
		}
		if (!(item.getItem() instanceof ItemProp)) {
			logger.error("item: {} isn't an useable item.");
			return SFSObject.newInstance();
		}
		ItemProp useable = (ItemProp) item.getItem();
		ISFSObject ref = useable.use(userProfile, params);
		manager.operItemCount(item, -1);
		return ref;
	}

	/**
	 * 合成物品
	 */
	public static final ItemValue compositeItem(UserProfile userProfile, String itemId) throws WW2Exception {
		// 合成表以及资源
		ISFSObject comTable = ItemConfig.getItemComTable(itemId);
		ISFSObject comRes = ItemConfig.getItemComResource(itemId);
		if (!userProfile.getResManager().isResEnoughMulti(comRes)) {
			throw new WW2Exception(WW2ExceptionCode.USER_RES_NOT_ENOUGH);
		}
		UserInventoryManager inventory = userProfile.getInventory();
		if (!inventory.isItemEnough(comTable)) {
			throw new WW2Exception(WW2ExceptionCode.USER_ITEM_NOT_ENOUGH);
		}
		// 扣资源
		userProfile.getResManager().operResWithMaxCheck(comRes, false);
		userProfile.getResManager().update();
		userProfile.getResManager().sync();
		// 扣物品
		inventory.subItems(comTable);
		// 生成新的物品(此处返回值不一定是append内的)
		return inventory.addItem(itemId, 1);
	}

	/**
	 * 升级装备
	 */
	public static final void upgradeEquipment(UserProfile userProfile, String uuid, String armyId, String grid)
			throws WW2Exception {
		UserInventoryManager inventory = userProfile.getInventory();
		ItemValue origin_iv = inventory.getItemByUUID(uuid);
		UserArmyManager armyManager = userProfile.getArmyManager();
		UserArmy userArmy = armyManager.getArmy(armyId);
		if (userArmy == null) {
			throw new WW2Exception();
		}
		if (origin_iv == null) {
			throw new WW2Exception();
		}
		String originItemId = origin_iv.getItem().getItemId();
		// 物品等级不大于对应兵的等级
		int lv = ItemConfig.getItemLv(originItemId);
		if (lv >= userArmy.getLevel()) {
			throw new WW2Exception();
		}

		String nextItemId = ItemConfig.getItemNextId(originItemId);
		if (StringUtils.isBlank(nextItemId)) {
			throw new WW2Exception(WW2ExceptionCode.LEVEL_LIMIT); // 最大等级
		}
		ItemValue new_iv = compositeItem(userProfile, nextItemId);
		if (new_iv == null) {
			throw new WW2Exception(WW2ExceptionCode.FAILURE);
		}
		// 将原装备去除
		userProfile.getInventory().operItemCount(origin_iv, -1);
		// 再装备上去
		userProfile.getArmyManager().armyEquip(armyId, grid, new_iv.getItem().getUuid());
	}

	/**
	 * 合成并装备
	 */
	public static final void compositeAndEquip(UserProfile userProfile, String itemId, String armyId, String grid)
			throws WW2Exception {
		// 先合成一个物品
		ItemValue new_iv = ItemService.compositeItem(userProfile, itemId);
		if (new_iv == null) {
			throw new WW2Exception(WW2ExceptionCode.FAILURE);
		}
		// 再装备上去
		userProfile.getArmyManager().armyEquip(armyId, grid, new_iv.getItem().getUuid());
	}
}
